/**
 * 获取点击事件代理对象
 * 
 * @param e 
 * @returns 
 */
export function getTarget<H extends HTMLElement>(e: Event): H {
  const tar = e.target || e.srcElement;
  return tar as H;
}

/**
 * 点击事件中 获取事件代理对象的所有类名
 * 
 * @param tar 
 * @returns 
 */
export function getClassNames(tar: Element): string[] {
  return Array.from(tar.className.split(' '));
}

/**
 * 判断给定元素是否存在给定的类名
 * 
 * @param tar HTMLElement
 * @param className string
 * @returns 
 */
export function hasClass(tar: Element, className: string): boolean {
  const cls: string[] = Array.from(tar.className.split(' '));
  console.log(cls.includes(className))
  return cls.includes(className);
}

/**
 * 移除类名
 * 
 * @param tar HTMLElement
 * @param clsName string
 */
export function removeClass(tar: HTMLElement, clsName: string) {
  const cls = tar.className.split(' ');
  const newCls = cls.filter(clsItem => {
    return clsItem != clsName ? clsItem : "";
  }).join(' ');
  tar.className = newCls;
}

/**
 * 添加类名
 * 
 * @param tar HTMLElement
 * @param clsName string
 */
export function addClass(tar: HTMLElement, clsName: string) {
  tar.className += ` ${clsName}`;
}